using System;
using System.ComponentModel;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Controls;

namespace FourRoads.Links.Controls
{
	public abstract class ResLinkBaseSectionView : ResLinkBaseTemplatedWebControl
	{
		protected ResLinkConfiguration _Config = ResLinkConfiguration.Instance();

		protected string FormatSectionLinkCounts(ResLinkSection rls)
		{
			ResLinkSection fullSection = ResLinkSection.GetSection(rls.SectionID);

			if (_Config.DisplaySectionCounts && _Config.DisplayLinkCounts)
			{
				return string.Format(ResourceManager.GetString("ResLinkSections_DisplaySectionCounters")   ,  fullSection.Name , fullSection.AdjustedSections.Count , fullSection.FullSubLinkCount);
			}
			else if (_Config.DisplaySectionCounts)
			{
				return string.Format(ResourceManager.GetString("ResLinkSections_DisplaySingleSectionCounter")   ,  fullSection.Name , fullSection.AdjustedSections.Count);
			}

			return string.Format(ResourceManager.GetString("ResLinkSections_DisplaySingleSectionCounter")   ,  fullSection.Name , fullSection.FullSubLinkCount);
		}
	}

	/// <summary>
	/// Summary description for ResLinkLargeSectionView.
	/// </summary>
	public class ResLinkLargeSectionView : ResLinkBaseSectionView
	{
		#region Child Controls
			RepeaterPlusNone  _MainSections = null;
		#endregion

		protected override void AttachChildControls()
		{
			
			_MainSections = FindControl("MainSections") as RepeaterPlusNone;
			_MainSections.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(_MainSections_ItemDataBound);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if (!Page.IsPostBack)
			{
				DataBind();
			}
		}

		public override void DataBind()
		{
			base.DataBind ();

			_MainSections.DataSource = ThisSection.AdjustedSections;
			_MainSections.DataBind();
		}

		private ResLinkSection BindCurrentItem(string hyperLinkName , RepeaterItemEventArgs e)
		{
			HyperLink hyp = e.Item.FindControl(hyperLinkName) as HyperLink;
			ResLinkSection rls = ((ResLinkSection)e.Item.DataItem);		

			if (_Config.DisplaySectionCounts || _Config.DisplayLinkCounts)
			{
				hyp.Text = hyp.Text = FormatSectionLinkCounts(rls);
			}
			else
			{
				hyp.Text = rls.Name;
			}

			if (rls.IsActive)
			{
				hyp.NavigateUrl = ResLinkUrls.Instance().ViewSection( System.Convert.ToInt32(rls.SectionID) );
				hyp.ToolTip = string.Format(ResourceManager.GetString("ShowResourceLinks_ToolTip") ,  rls.Name);
			}
			else
			{
				hyp.ToolTip = ResourceManager.GetString("ShowResourceLinks_SectionClosed");
			}

			return rls;

		}

		private void _MainSections_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || 
				e.Item.ItemType == ListItemType.AlternatingItem )
			{
				ResLinkSection rls = BindCurrentItem("SectionNameLnk" , e);
				
				//Ensure we have a full 
				rls = ResLinkSection.GetSection(rls.SectionID);

				//Now deal with the inner repeater
				RepeaterPlusNone rpt = e.Item.FindControl("SubSections") as RepeaterPlusNone;

				rpt.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(rpt_ItemDataBound);
				rpt.DataSource = rls.AdjustedSections;
				rpt.DataBind();

				Literal finishingText =  e.Item.FindControl("FinishingText") as Literal;
				if (finishingText != null)
				{
					finishingText.Visible = rls.AdjustedSections.Count > 0;
				}
			}
		}

		[DefaultValue(3)]
		public int NumberSubSections
		{
			get
			{
				if (ViewState["NumberSubSections"] == null)
				{
					AttributeCollection attributes = TypeDescriptor.GetProperties(this)["NumberSubSections"].Attributes;
				
					DefaultValueAttribute defValue = (DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];

					ViewState["NumberSubSections"] = defValue.Value;
				}
				return (int)ViewState["NumberSubSections"];
			}
			set
			{
				ViewState["NumberSubSections"] = value;
			}
		}
		//Databinding 
		private void rpt_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || 
				e.Item.ItemType == ListItemType.AlternatingItem )
			{
				if (e.Item.ItemIndex < NumberSubSections)
				{
					ResLinkSection rls = BindCurrentItem("SubSectionLnk" , e);
				}
				else
				{
					e.Item.Visible = false;
				}
			}
			else if (e.Item.ItemType == ListItemType.Separator)
			{
				if (e.Item.ItemIndex >= NumberSubSections-1)
				{
					e.Item.Visible = false;
				}
			}
		}
	}
}
